<?php
/**
 * @version 1.0
 * @since June-2008
 * @package lib
 * @subpackage data
 * @name CTable
 * @deprecated Lớp thao tác với một bảng/đối tượng trong cơ sở dữ liệu
*/
class CTable
{
	public $store;
	public $table;
	public $record;
	public $original_record;
	public $inserted_id;
	public $options = array();
	public $condition = false;
	function __construct($table, $record = false)
	{
		if(is_object($table))
		{
			$this->store = $table;
		}
		else
		{
			$this->table = $table;
			$this->store = &CStore::$default;
			$this->record = $this->original_record =
				is_object($record)?
					$record:(
						is_array($record)?
							CRecord($record):(
								$record?
									CRecord($this->store->select($table,EId($record))):false));
		}
	}
	function &no_cache($value = 1)
	{
		$this->options['no_cache'] = $value;
		return $this;
	}
	function &master()
	{
		$this->options['slave'] = false;
		return $this;
	}
	function options($options = array())
	{
		return array_merge(is_array($options)?$options:array(), is_array($this->options)?$this->options:array());
	}
	function &condition($condition = array())
	{
		$this->condition = $condition;
		return $this;
	}
	function get_condition($condition = array())
	{
		if($this->condition)
		{
			if($condition)
			{
				$condition =  EAnd($this->condition, $condition);
			}
			else
			{
				$condition =  $this->condition;
			}
		}
		$this->condition = false;
		return $condition;
	}
	function &order_by($order_by)
	{
		$this->options['order_by'] = $order_by;
		return $this;
	}
	function &items_per_page($items_per_page)
	{
		$this->options['items_per_page'] = $items_per_page;
		return $this;
	}
	function &fields($fields)
	{
		$this->options['fields'] = $fields;
		return $this;
	}

	function &cache($cache_time) {
	  $this->options['cache'] = $cache_time;
	  return $this;
	}

	function &page_no($page_no)
	{
		$this->options['page_no'] = $page_no;
		return $this;
	}
	function &group_by($group_by)
	{
		$this->options['group_by'] = $group_by;
		return $this;
	}
	function &join($table, $condition = false, $type = '')
	{
		if(!isset($this->options['joins']))
		{
			$this->options['joins'] = array();
		}
		$this->options['joins'][] = array(
			'table' => $table,
			'condition' => $condition?$condition:$table.'_id',
			'type' => $type
		);
		return $this;
	}
	function &left_join($table, $condition = false)
	{
		return $this->join($table, $condition, 'LEFT OUTER');
	}
	/**
	 * Phương thức thay đổi store
	 *
	 * @param store mới $store
	 * @return store sau khi đã thay đổi
	 */
	function &change_store(&$store) {
		$this->store = &$store;
		return $this;
	}

	/**
	 *
	 * @param unknown_type $condition
	 * @param unknown_type $options
	 * @return int Number of rows in table
	 */
	function count($condition, $options=array()) {
		return $this->store->count($this->table, $this->get_condition($condition), $options);
	}

	function sum($fieldname, $conditions = false){
		return $this->fields('sum('.$fieldname.') as sum')->select_value($conditions);
	}

	/**
	 * Phương thức lấy ra bản ghi đầu tiên trong bảng
	 *
	 * @param điều kiện lựa chọn các bản ghi $condition kiểu array
	 * @param tùy chọn cách lấy bản ghi như các trường, cách sắp xếp,... $options kiểu array
	 * @return nội dung bản ghi lấy ra được
	 */
	function select($condition = false, $options = array()) {
		if(!$this->record) {
			$this->record = CRecord();
		}
		if(!$this->original_record)
		{
			$this->original_record = CRecord();
		}
		if(is_array($options)){
			$options = $options + array('items_per_page' => 1);
		}
		$this->record->data
			= $this->original_record->data
			= $this->store->select($this->table, $this->get_condition($condition), $this->options($options));
		return $this->record->data;
	}
	function select_all($conditions = false, $options = array())
	{
		return $this->store->select_all($this->table, $this->get_condition($conditions), $this->options($options));
	}
	function update($values, $conditions = false)
	{
		return $this->store->update($this->table, $values, $this->get_condition($conditions));
	}
	function insert($values)
	{
		return ($this->inserted_id = $this->store->insert($this->table, $values));
	}
	function &save($data = false)
	{
		if($data)
		{
			$this->record = new CRecord($data);
		}
		if($this->original_record and $this->original_record->data)
		{
			$this->store->update($this->table,
				$this->record->data, $this->original_record->id);
			if(isset($this->store->relationships[$this->table]) and ($this->original_record->id != $this->record->id))
			{
				$this->store->cascade_update($this->table, $this->original_record->id, $this->record->id);
			}
		}
		else
		if($this->record and $this->record->data)
		{
		      $this->inserted_id= $this->store->insert($this->table, $this->record->data);
		}

		return $this;
	}
	function &delete($id = false)
	{
		if($id)
		{

			$this->store->delete($this->table, $id);
		}
		else
			if($this->original_record)
				{
					$this->store->delete($this->table, $this->original_record->id);
					$id = $this->original_record->id;
				}
				if(isset($this->store->relationships[$this->table]))
				{
					$this->store->cascade_delete($this->table, $id);
				}

			return $this;
	}
	function delete_all($condition = false)
	{
		$this->store->delete($this->table, $this->get_condition($condition));
	}
	function select_value($conditions = false, $options = array())
	{
		if($item = $this->select($conditions, $options))
		{
			return reset($item);
		}
	}
}
/**
 * Enter description here...
 *
 * @param string $table
 * @param mixed $record
 * @return CTable
 */
function CTable($table, $record = false)
{
	return new CTable($table, $record);
}
?>